var curFileObj = '';
var oriFileContent = '';
var editor = '';

function initCodeMirror() {
    var mixedMode = {
        name: "htmlmixed",
        scriptTypes: [{
                matches: /\/x-handlebars-template|\/x-mustache/i,
                mode: null
            },
            {
                matches: /(text|application)\/(x-)?vb(a|script)/i,
                mode: "vbscript"
            }
        ]
    };
    editor = CodeMirror.fromTextArea(document.getElementById("code"), {
        mode: mixedMode,
        selectionPointer: true,
        lineNumbers: true,
        //theme: "",//night
    });
    oriFileContent = editor.getValue();
}


navbarApp.controller('contest_file_list_controller', function ($scope, $http) {
    $scope.contestId = contestId;
    $scope.contestRefer = contestRefer;
    $scope.fileName = fileName;
    $scope.fileSize = fileSize;

    ////測試一下用ajax取文件内容可行否--> 也是可行的
    //$http.post("/contests/contest/get_file_info",
    //    {
    //        contestId: contestId,
    //        filePath: contestRefer,
    //        fileName: fileName
    //    }).success(function (response) {
    //        if (response.status == '200') {
    //            curFileObj = response.data;
    //            var ss = curFileObj.content;
    //            console.log(ss);
    //            console.log(response);
    //
    //        } else {
    //            layer.msg('服务器错误', {
    //                icon: 2,
    //                time: 2000 //2秒关闭（如果不配置，默认是3秒）
    //            }, function () {
    //                //do something
    //
    //            });
    //        }
    //    }).error(function (response) {
    //    layer.msg(response, {
    //        icon: 1,
    //        time: 2000 //2秒关闭（如果不配置，默认是3秒）
    //    }, function () {
    //        //do something
    //
    //    });
    //})

    //绑定文本框输入的事件: 不允许输入 '/'
    $scope.handleInputKeyUp = function(event) {
        console.log(88989);
        var event = window.event || event;
        event.srcElement = event.srcElement ? event.srcElement : event.target;
        var srcObj = $(event.srcElement);
        var code = event.keyCode || event.which;
        console.log(code);
        if(code == 111 || code == 191) {
            event.preventDefault();
            return false;
        }
        //$scope.fileName = srcObj.val().replace(/\//g,'');
        //console.log($scope.fileName);
    }

    $scope.cancelSet = function() {
        layer.confirm('取消后,编辑的内容将被重置确定取消?', {icon: 3, title:'提示'}, function(index){
            layer.close(index);
            editor.setValue(oriFileContent);
        });
    }
    $scope.resetFileName = function() {
        layer.confirm('要恢复原文件名么?', {icon: 3, title:'提示'}, function(index){
            layer.close(index);
            $scope.$apply(function() {
                $scope.fileName = fileName;
            })

        });
    }

    $scope.saveContent = function() {
        var latestContent = editor.getValue();
        layer.confirm('确定提交么?', {icon: 3, title:'提示'}, function(index){
            layer.close(index);
            if(latestContent == oriFileContent) {
                layer.msg('内容没有修改,不需要提交',{icon:6});
                return false;
            }
            var index = layer.load(2);//loading...
            $.ajax({
                // 提交数据的类型 POST GET
                type : "post",
                // 提交的网址
                url : '/contests/contest/set_file_content',
                // 提交的数据
                data : {
                    filePath: $scope.contestRefer,
                    fileNameOld: $scope.fileName,
                    fileNameNew: fileName,
                    fileContent: latestContent
                },
                dataType : "json",// "xml", "html", "script", "json",
                success : function(data) {
                    layer.close(index);
                    if(data.status == 200){
                        layer.msg(data.msg, {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        }, function(){
                            //do something
                        });
                    }else if(data.status == '302'){
                        layer.msg('您没有该权限');
                    }else {
                        layer.msg(data.msg, {
                            icon: 2,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        })
                    }
                },
                error : function(e) {
                }
            });
            layer.close(index);
        });
    }



    initCodeMirror();



});










